package com.luca.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luca.system.domain.FxhPlate;
import com.luca.system.mapper.FxhPlateMapper;
import com.luca.system.service.IFxhPlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 非小号板块数据Service业务层处理
 *
 * @author luca
 * @date 2021-04-07
 */
@Service
public class FxhPlateServiceImpl extends ServiceImpl<FxhPlateMapper, FxhPlate> implements IFxhPlateService {

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 查询非小号板块数据
     *
     * @param id 非小号板块数据ID
     * @return 非小号板块数据
     */
    @Override
    public FxhPlate selectFxhPlateById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询非小号板块数据列表
     *
     * @param fxhPlate 非小号板块数据
     * @return 非小号板块数据
     */
    @Override
    public List<FxhPlate> selectFxhPlateList(FxhPlate fxhPlate) {
        var query = new QueryWrapper<FxhPlate>();
        return baseMapper.selectList(query);
    }

    /**
     * 新增非小号板块数据
     *
     * @param fxhPlate 非小号板块数据
     * @return 结果
     */
    @Override
    public int insertFxhPlate(FxhPlate fxhPlate) {
        return baseMapper.insert(fxhPlate);
    }

    /**
     * 修改非小号板块数据
     *
     * @param fxhPlate 非小号板块数据
     * @return 结果
     */
    @Override
    public int updateFxhPlate(FxhPlate fxhPlate) {
        return baseMapper.updateById(fxhPlate);
    }

    /**
     * 批量删除非小号板块数据
     *
     * @param ids 需要删除的非小号板块数据ID
     * @return 结果
     */
    @Override
    public int deleteFxhPlateByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除非小号板块数据信息
     *
     * @param id 非小号板块数据ID
     * @return 结果
     */
    @Override
    public int deleteFxhPlateById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public void pull(Map data) {
        baseMapper.delete(null);
        var saveList = new ArrayList<FxhPlate>();
        var list = (List) data.get("data");
        list.forEach(item -> {
            FxhPlate fxhPlate = null;
            try {
                fxhPlate = objectMapper.readValue(objectMapper.writeValueAsString(item), FxhPlate.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            saveList.add(fxhPlate);
        });
        saveBatch(saveList);
    }
}
